package com.yummy.web.security.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.yummy.security.RespectableUser;

public class RequestContext {

	public final static String SESSION_KEY = "sessionid";
	
	public final static String NEED_LOGIN = "need_login";
	
	public final static String CURRENT_USER = "user";
	
	private final static ThreadLocal<Session> subjectHolder = new ThreadLocal<>();
	
	public static RespectableUser getCurrentUser() {
		Session session = subjectHolder.get();
		if(session == null) {
			return null;
		}
		return (RespectableUser) session.getAttribute(CURRENT_USER);
	}
	
	public static void setSession(Session session) {
		subjectHolder.set(session);
	}
	
	public static Session getSession() {
		return subjectHolder.get();
	}

	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
	}

	public static void clear() {
		subjectHolder.remove();
	}
	
}
